package com.blogSystem.admin.controller;

import com.alibaba.fastjson.JSONObject;
import com.blogSystem.admin.pojo.Admin;
import com.blogSystem.admin.service.AdminService;
import entity.Result;
import entity.StatusCode;
import org.apache.tomcat.util.http.fileupload.FileUploadBase;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;
import util.JwtUtil;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("/admin")
public class AdminController {
   final public static int SIZE=10;
   @Autowired
    private JwtUtil jwtUtil;
    @Autowired
    private AdminService adminService;
    @RequestMapping(value = "/super/add",method = RequestMethod.POST)
    public Result add(@RequestBody Admin admin){
        if(adminService.searchAdmin(admin)!=null){
            return new Result(false, StatusCode.HAS, "管理员已存在");
        }
        adminService.setAdmin(admin);
        return new Result(true, StatusCode.OK, "添加成功");
    }
    @RequestMapping(value = "/super/delete",method = RequestMethod.POST)
    public Result delete(@RequestBody String id){
        adminService.delete(id);
        return new Result(true, StatusCode.OK, "删除成功");
    }
    @RequestMapping(value = "/super/one",method = RequestMethod.POST)
    public Result add(@RequestBody String id){
        Admin admin=adminService.findById(id);
        return new Result(true, StatusCode.OK, "查询成功",admin);
    }
    @RequestMapping(value = "/super/list/{page}",method = RequestMethod.POST)
    public Result list(@PathVariable int page){
        System.out.println("page:"+page);
        Page<Admin> pages=adminService.findAll(page,SIZE);
        return new Result(true, StatusCode.OK, "查询成功",pages);
    }
    @RequestMapping(value = "/super/search/{page}",method = RequestMethod.POST)
    public Result search(@RequestBody Admin admin,@PathVariable int page){


        Page<Admin> pages=adminService.findSearch(admin,page,SIZE);
        return new Result(true, StatusCode.OK, "查询成功",pages);
    }
    @RequestMapping(value = "/super/changename",method = RequestMethod.POST)
    public Result changename(@RequestBody Map map){
        String id= (String) map.get("id");
        LinkedHashMap linkedHashMap = (LinkedHashMap) map.get("admin");
        String json=new JSONObject().toJSONString(linkedHashMap);
        Admin newAdmin= JSONObject.parseObject(json,Admin.class);

       if (adminService.changeName(id,newAdmin)) {
            return new Result(true, StatusCode.OK, "修改成功");
        }
       else
           return new Result(false, StatusCode.HAS, "用户名已存在");
    }
    @RequestMapping(value = "/super/changepass",method = RequestMethod.POST)
    public Result changepass(@RequestBody Map map){
        String id= (String) map.get("id");
        LinkedHashMap linkedHashMap = (LinkedHashMap) map.get("admin");
        String json=new JSONObject().toJSONString(linkedHashMap);
        Admin newAdmin= JSONObject.parseObject(json,Admin.class);

        if (adminService.changePass(id,newAdmin)) {
            return new Result(true, StatusCode.OK, "修改成功");
        }
        else
            return new Result(false, StatusCode.ERROR, "修改失败");
    }
    @RequestMapping(value = "/super/deletelist",method = RequestMethod.POST)
    public Result deletelist(@RequestBody List<String> ids){
        adminService.deleteList(ids);
            return new Result(true, StatusCode.OK, "删除成功");

    }
    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public Result login(@RequestBody Admin admin){
        Admin adminLogin=adminService.login(admin);
        if(adminLogin==null){
            return new Result(false , StatusCode.LOGINERROR, "用户名或密码错误");
        }
        else {
            if (adminLogin.getRole() == 0) {
                String token=jwtUtil.createJWT(adminLogin.getId(), adminLogin.getName(), "super");
                Map<String,Object> map=new HashMap<String,Object>();
                map.put("token",token);
                map.put("role","super");
                return new Result(true, StatusCode.OK, "登录成功",map);
            }
            else if(adminLogin.getRole() == 1) {
                String token=jwtUtil.createJWT(adminLogin.getId(), adminLogin.getName(), "admin");
                Map<String,Object> map=new HashMap<String,Object>();
                map.put("token",token);
                map.put("role","admin");
                return new Result(true, StatusCode.OK, "登录成功",map);
            }
        }
        return new Result(false , StatusCode.LOGINERROR, "用户名或密码错误");
    }
}